﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SponsorshipManager.DataAccess
{
    public class Database
    {
        

        public enum DatabaseProviderEnum {
            Unknown = -1,
            None = 0,
            SqlClient,
            Oracle,
            MySql,
            PostGres,
            ODBC,
            Test
        }


        IDbProvider _DbProvider;
        public DatabaseProviderEnum DbType {get; set;}
        public string ConnectionString { get; set; }


        public bool Connection {
            get {
                return _DbProvider.Connected;
            }

            set {
                _DbProvider.Connected = value;
            }
        }

        public Database(DatabaseProviderEnum DBType, string connectionString) 
        {
            this.DbType = DBType;
            this.ConnectionString = connectionString;
            _DbProvider = GetProvider();
        }

        public void Initialize(){
            _DbProvider= GetProvider();
        }

        private IDbProvider GetProvider(){
            IDbProvider ret;
            switch(DbType){

                case DatabaseProviderEnum.Unknown:
                    throw new NotImplementedException();

                case DatabaseProviderEnum.SqlClient:
                    ret = (IDbProvider)  new ProviderSqlServer();
                    break;
            
                case DatabaseProviderEnum.Oracle:
                    ret = (IDbProvider) new ProviderOracle();
                    break;
                case DatabaseProviderEnum.Test:
                    ret = (IDbProvider)new ProviderTest();
                    break;
                default:
                    throw new NotImplementedException();
            }
            ret.ConnectionString = this.ConnectionString;
            return ret;

        }

        public StoredProcedure StoredProcedure()
        {
            StoredProcedure ret = new StoredProcedure(_DbProvider);
            return ret;
        }
    }
}
